home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_570 / gadtoolsbox / source / source.lha / SetPalette.c < prev    next >
C/C++ Source or Header  |  1991-11-04  |  11KB  |  324 lines

  1. /*-- AutoRev header do NOT edit!
  2. *
  3. *   Program         :   SetPalette.c
  4. *   Copyright       :   © Copyright 1991 Jaba Development
  5. *   Author          :   Jan van den Baard
  6. *   Creation Date   :   22-Sep-91
  7. *   Current version :   1.00
  8. *   Translator      :   DICE v2.6
  9. *
  10. *   REVISION HISTORY
  11. *
  12. *   Date          Version         Comment
  13. *   ---------     -------         ------------------------------------------
  14. *   22-Sep-91     1.00            Screen palette editor.
  15. *
  16. *-- REV_END --*/
  17.  
  18. #include "GTEd.h"
  19. #include "Protos.h"
  20.  
  21. /*
  22.  * --- External refrenced data.
  23.  */
  24. extern ULONG             Class;
  25. extern UWORD             Code;
  26. extern struct Gadget    *Object;
  27. extern APTR              MainVisualInfo;
  28. extern struct TextAttr   Topaz80;
  29. extern struct Screen    *MainScreen;
  30. extern struct Window    *MainWindow;
  31. extern struct ColorSpec  MainColors[33];
  32. extern BOOL              Saved;
  33.  
  34. /*
  35.  * --- Palette window gadget ID's
  36.  */
  37. #define GD_RED           0
  38. #define GD_GREEN         1
  39. #define GD_BLUE          2
  40. #define GD_PALETTE       3
  41. #define GD_OK            4
  42. #define GD_RESET         5
  43. #define GD_CANCEL        6
  44.  
  45. /*
  46.  * --- Program gadget pointers that needs to be changed.
  47.  */
  48. struct Gadget           *sp_Red, *sp_Green, *sp_Blue, *sp_Palette;
  49.  
  50. struct Window           *sp_Wnd   = NULL;
  51. struct Gadget           *sp_GList = NULL;
  52. UWORD                    sp_Orig[32];
  53. UWORD                    sp_Rc, sp_Gc, sp_Bc;
  54. WORD                     sp_Zoom[4];
  55.  
  56. /*
  57.  * --- TagItems for the palette window.
  58.  */
  59. struct TagItem  sp_nwTags[] = {
  60.     WA_Left,                0l,
  61.     WA_Top,                 0l,
  62.     WA_Width,               0l,
  63.     WA_Height,              0l,
  64.     WA_IDCMP,               IDCMP_CLOSEWINDOW | SLIDERIDCMP | PALETTEIDCMP | BUTTONIDCMP | IDCMP_REFRESHWINDOW | IDCMP_VANILLAKEY,
  65.     WA_Flags,               WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH | WFLG_ACTIVATE | WFLG_RMBTRAP,
  66.     WA_Gadgets,             0l,
  67.     WA_Title,               "Screen Palette",
  68.     WA_AutoAdjust,          TRUE,
  69.     WA_Zoom,                (Tag)sp_Zoom,
  70.     WA_CustomScreen,        0l,
  71.     TAG_DONE };
  72.  
  73. /*
  74.  * --- TagItems for the slider gadgets.
  75.  */
  76. struct TagItem  sp_PTags[] = {
  77.     GTSL_LevelFormat,       "%2ld",
  78.     GTSL_MaxLevelLen,       2L,
  79.     TAG_DONE };
  80.  
  81. /*
  82.  * --- Set the slider levels accoording to color register 'reg'.
  83.  */
  84. void SetProp( long reg )
  85. {
  86.     UWORD   r, g, b, col;
  87.  
  88.     col = GetRGB4( MainScreen->ViewPort.ColorMap, reg );
  89.  
  90.     r = sp_Rc = (( col >> 8 ) & 0x0f );
  91.     g = sp_Gc = (( col >> 4 ) & 0x0f );
  92.     b = sp_Bc = ( col & 0x0f );
  93.  
  94.     GT_SetGadgetAttrs( sp_Red,   sp_Wnd, NULL, GTSL_Level, r, TAG_DONE );
  95.     GT_SetGadgetAttrs( sp_Green, sp_Wnd, NULL, GTSL_Level, g, TAG_DONE );
  96.     GT_SetGadgetAttrs( sp_Blue,  sp_Wnd, NULL, GTSL_Level, b, TAG_DONE );
  97. }
  98.  
  99. /*
  100.  * --- Open the palette editor and wait for user input.
  101.  */
  102. long SetPalette( void )
  103. {
  104.     struct Gadget       *g;
  105.     struct NewGadget     ng;
  106.     BOOL                 running  = TRUE;
  107.     WORD                 reg = 0, l, t, w, h, btop, bleft, r;
  108.     UWORD                col;
  109.  
  110.     btop  = MainScreen->WBorTop + 1 + MainScreen->RastPort.TxHeight;
  111.     bleft = MainScreen->WBorLeft;
  112.  
  113.     w = bleft + MainScreen->WBorRight  + 302;
  114.     h = btop  + MainScreen->WBorBottom + 94;
  115.     l = (( MainScreen->Width  >> 1 ) - ( w >> 1 ));
  116.     t = (( MainScreen->Height >> 1 ) - ( h >> 1 ));
  117.  
  118.     sp_Zoom[0] = 0;
  119.     sp_Zoom[1] = btop;
  120.     sp_Zoom[2] = 200;
  121.     sp_Zoom[3] = btop;
  122.  
  123.     sp_nwTags[0].ti_Data = l;
  124.     sp_nwTags[1].ti_Data = t;
  125.     sp_nwTags[2].ti_Data = w;
  126.     sp_nwTags[3].ti_Data = h;
  127.  
  128.     sp_nwTags[10].ti_Data = (Tag)MainScreen;
  129.  
  130.     if (( MainScreen->Flags & CUSTOMSCREEN ) == CUSTOMSCREEN )
  131.         sp_nwTags[10].ti_Tag  = WA_CustomScreen;
  132.     else if (( MainScreen->Flags & PUBLICSCREEN ) == PUBLICSCREEN )
  133.         sp_nwTags[10].ti_Tag  = WA_PubScreen;
  134.     else
  135.         sp_nwTags[10].ti_Tag = TAG_DONE;
  136.  
  137.     for ( r = 0; r < ( 1L << MainScreen->BitMap.Depth ); r++ )
  138.         sp_Orig[r] = GetRGB4( MainScreen->ViewPort.ColorMap, (long)r );
  139.  
  140.     if ( g = CreateContext( &sp_GList )) {
  141.  
  142.         ng.ng_LeftEdge      =   bleft + 79;
  143.         ng.ng_TopEdge       =   btop + 3;
  144.         ng.ng_Width         =   216;
  145.         ng.ng_Height        =   10;
  146.         ng.ng_GadgetText    =   "Red     ";
  147.         ng.ng_TextAttr      =   &Topaz80;
  148.         ng.ng_GadgetID      =   GD_RED;
  149.         ng.ng_Flags         =   PLACETEXT_LEFT | NG_HIGHLABEL;
  150.         ng.ng_VisualInfo    =   MainVisualInfo;
  151.         ng.ng_UserData      =   NULL;
  152.  
  153.         g = CreateGadgetA( SLIDER_KIND, g, &ng, sp_PTags );
  154.  
  155.         sp_Red = g;
  156.  
  157.         ng.ng_TopEdge       =   btop + 17;
  158.         ng.ng_GadgetText    =   "Green   ";
  159.         ng.ng_GadgetID      =   GD_GREEN;
  160.  
  161.         g = CreateGadgetA( SLIDER_KIND, g, &ng, sp_PTags );
  162.  
  163.         sp_Green = g;
  164.  
  165.         ng.ng_TopEdge       =  btop +  31;
  166.         ng.ng_GadgetText    =   "Blue    ";
  167.         ng.ng_GadgetID      =  GD_BLUE;
  168.  
  169.         g = CreateGadgetA( SLIDER_KIND, g, &ng, sp_PTags);
  170.  
  171.         sp_Blue  = g;
  172.  
  173.         ng.ng_LeftEdge      =  bleft + 7;
  174.         ng.ng_TopEdge       =  btop + 45;
  175.         ng.ng_Width         =  288;
  176.         ng.ng_Height        =  30;
  177.         ng.ng_GadgetText    =  0L;
  178.         ng.ng_GadgetID      =  GD_PALETTE;
  179.         ng.ng_Flags         =  0;
  180.  
  181.         g = CreateGadget( PALETTE_KIND, g, &ng, GTPA_Depth, (Tag)MainScreen->BitMap.Depth, GTPA_IndicatorWidth, 64l, GTPA_Color, (Tag)reg, TAG_DONE );
  182.  
  183.         ng.ng_TopEdge       =   btop + 79;
  184.         ng.ng_Width         =   64;
  185.         ng.ng_Height        =   12;
  186.         ng.ng_GadgetText    =   "_OK";
  187.         ng.ng_GadgetID      =   GD_OK;
  188.         ng.ng_Flags         =   PLACETEXT_IN;
  189.  
  190.         g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, (Tag)'_', TAG_DONE );
  191.  
  192.         ng.ng_LeftEdge      =   bleft + 120;
  193.         ng.ng_GadgetText    =   "_Reset";
  194.         ng.ng_GadgetID      =   GD_RESET;
  195.  
  196.         g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, (Tag)'_', TAG_DONE );
  197.  
  198.         ng.ng_LeftEdge      =   bleft + 231;
  199.         ng.ng_GadgetText    =   "_CANCEL";
  200.         ng.ng_GadgetID      =   GD_CANCEL;
  201.  
  202.         g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, (Tag)'_', TAG_DONE );
  203.  
  204.         if ( g ) {
  205.  
  206.             sp_nwTags[6].ti_Data    =   (Tag)sp_GList;
  207.  
  208.             if ( sp_Wnd = OpenWindowTagList( NULL, sp_nwTags )) {
  209.  
  210.                 sp_Zoom[0] = l;
  211.                 sp_Zoom[1] = t;
  212.                 sp_Zoom[2] = w;
  213.                 sp_Zoom[3] = h;
  214.  
  215.                 GT_RefreshWindow( sp_Wnd, NULL );
  216.                 SetProp( reg );
  217.  
  218.                 do {
  219.                     WaitPort( sp_Wnd->UserPort );
  220.  
  221.                     while ( ReadIMsg( sp_Wnd )) {
  222.  
  223.                         switch ( Class ) {
  224.  
  225.                             case    IDCMP_REFRESHWINDOW:
  226.                                 GT_BeginRefresh( sp_Wnd );
  227.                                 GT_EndRefresh( sp_Wnd, TRUE );
  228.                                 break;
  229.  
  230.                             case    IDCMP_CLOSEWINDOW:
  231.                                 goto Cancel;
  232.                                 break;
  233.  
  234.                             case    IDCMP_VANILLAKEY:
  235.  
  236.                                 switch ( Code ) {
  237.  
  238.                                     case    'o':
  239.                                         goto Ok;
  240.  
  241.                                     case    'r':
  242.                                         goto Reset;
  243.  
  244.                                     case    'c':
  245.                                         goto Cancel;
  246.  
  247.                                 }
  248.                                 break;
  249.  
  250.                             case    IDCMP_MOUSEMOVE:
  251.  
  252.                                 switch ( Object->GadgetID ) {
  253.  
  254.                                     case    GD_RED:
  255.                                         sp_Rc = Code;
  256.                                         goto Set;
  257.  
  258.                                     case    GD_GREEN:
  259.                                         sp_Gc = Code;
  260.                                         goto Set;
  261.  
  262.                                     case    GD_BLUE:
  263.                                         sp_Bc = Code;
  264.  
  265.                                         Set:
  266.                                         SetRGB4( &MainScreen->ViewPort, reg, sp_Rc, sp_Gc, sp_Bc );
  267.                                         break;
  268.                                 }
  269.                                 break;
  270.  
  271.                             case    IDCMP_GADGETUP:
  272.  
  273.                                     switch ( Object->GadgetID ) {
  274.  
  275.                                         case    GD_PALETTE:
  276.                                             reg = Code;
  277.                                             SetProp( (long)reg );
  278.                                             break;
  279.  
  280.                                         case    GD_OK:
  281.                                             Ok:
  282.                                             running = FALSE;
  283.                                             for ( r = 0; r < ( 1L << MainScreen->BitMap.Depth ); r++ ) {
  284.                                                 col = GetRGB4( MainScreen->ViewPort.ColorMap, (long)r );
  285.                                                 MainColors[r].ColorIndex = r;
  286.                                                 MainColors[r].Red        = (( col >> 8 ) & 0x0f );
  287.                                                 MainColors[r].Green      = (( col >> 4 ) & 0x0f );
  288.                                                 MainColors[r].Blue       = (( col      ) & 0x0f );
  289.                                             }
  290.                                             MainColors[r].ColorIndex = ~0;
  291.                                             Saved = FALSE;
  292.                                             break;
  293.  
  294.                                         case    GD_RESET:
  295.                                             Reset:
  296.                                             LoadRGB4( &MainScreen->ViewPort, sp_Orig, ( 1L << MainScreen->BitMap.Depth ));
  297.                                             SetProp( reg );
  298.                                             break;
  299.  
  300.                                         case    GD_CANCEL:
  301.                                             Cancel:
  302.                                             LoadRGB4( &MainScreen->ViewPort, sp_Orig, ( 1L << MainScreen->BitMap.Depth ));
  303.                                             running = FALSE;
  304.                                             break;
  305.                                     }
  306.                                     break;
  307.                         }
  308.                     }
  309.                 } while ( running );
  310.             }
  311.         }
  312.     }
  313.  
  314.     if ( sp_Wnd )           CloseWindow( sp_Wnd );
  315.     if ( sp_GList )         FreeGadgets( sp_GList );
  316.  
  317.     sp_GList = 0l;
  318.     sp_Wnd   = 0l;
  319.  
  320.     ClearMsgPort( MainWindow->UserPort );
  321.  
  322.     return TRUE;
  323. }
  324.